﻿using System;
using System.Globalization;

namespace XSerialization.Generator
{
    public interface IXsdTypeReference
    {
        /// <summary>
        /// Public Name
        /// </summary>
        string Name { get; set; }

        /// <summary>
        /// Is Property a XML Attribute
        /// </summary>
        bool IsAttribute { get; set; }

        /// <summary>
        /// Is Property required
        /// </summary>
        bool IsRequired { get; set; }

        /// <summary>
        /// Original XML Name
        /// </summary>
        string XmlName { get; set; }

        /// <summary>
        /// Is IList Property 
        /// </summary>
        bool IsList { get; set; }

        /// <summary>
        /// Simple or Complex Type
        /// </summary>
        bool IsSimpleType { get; set; }

        /// <summary>
        /// Xml Sewuence Index
        /// </summary>
        int Index { get; set; }

        /// <summary>
        /// Description
        /// </summary>
        string Description { get; set; }

        /// <summary>
        /// Type Interface Name
        /// </summary>
        string TypeInterfaceName { get; set; }

        /// <summary>
        /// Simple Type Name
        /// </summary>
        string TypeName { get; set; }

        /// <summary>
        /// Private Name
        /// </summary>
        string PrivateName { get; }

        /// <summary>
        /// Returns IsAttribute as string 
        /// </summary>
        /// <returns></returns>
        string GetIsAttributeAsString();

        /// <summary>
        /// Returns IsRequired as String
        /// </summary>
        /// <returns></returns>
        string GetIsRequiredAsString();
    }

    public class XsdTypeReference : IXsdTypeReference
    {
        /// <summary>
        /// Public Name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Is Property a XML Attribute
        /// </summary>
        public bool IsAttribute { get; set; }

        /// <summary>
        /// Is Property required
        /// </summary>
        public bool IsRequired { get; set; }

        /// <summary>
        /// Original XML Name
        /// </summary>
        public string XmlName { get; set; }

        /// <summary>
        /// Is IList Property 
        /// </summary>
        public bool IsList { get; set; }

        /// <summary>
        /// Simple or Complex Type
        /// </summary>
        public bool IsSimpleType { get; set; }

        /// <summary>
        /// Xml Sewuence Index
        /// </summary>
        public int Index { get; set; }

        /// <summary>
        /// Description
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Type Interface Name
        /// </summary>
        public string TypeInterfaceName { get; set; }

        /// <summary>
        /// Simple Type Name
        /// </summary>
        public string TypeName { get; set; }

        /// <summary>
        /// Private Name
        /// </summary>
        public string PrivateName
        {
            get
            {
                return "_" + Char.ToLowerInvariant(Name[0]) + Name.Substring(1);
            }
        }

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="name">Xml name</param>
        /// <param name="isAttribute">Is attribute object</param>
        /// <param name="isRequired">Is required</param>
        /// <param name="isList">Is list object</param>
        /// <param name="index">Xml index</param>
        /// <param name="desciption">Type description</param>
        public XsdTypeReference(string name, bool isAttribute, bool isRequired, bool isList, int index, string desciption)
        {
            Name = ConvertUtilities.ConvertXsdName(name);
            if (isList) Name = Name + "s";

            XmlName = name;
            IsAttribute = isAttribute;
            Index = index;
            IsList = isList;
            IsRequired = isRequired;
            Description = desciption;
            TypeInterfaceName = string.Empty;
            TypeName = string.Empty;
        }

        /// <summary>
        /// Returns IsAttribute as string 
        /// </summary>
        /// <returns>bool string</returns>
        public string GetIsAttributeAsString()
        {
            return IsAttribute.ToString(CultureInfo.InvariantCulture).ToLower();
        }

        /// <summary>
        /// Returns IsRequired as String
        /// </summary>
        /// <returns>bool string</returns>
        public string GetIsRequiredAsString()
        {
            return IsRequired.ToString(CultureInfo.InvariantCulture).ToLower();
        }
    }
}
